import Vue from 'vue'
import Vuex from 'vuex'

import patientStore from '../views/mobile/store';
import doctorStore from '../views/pc/store'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    ShopList: [],
    allPrice: '',
    allCount: '',
    xiang: [],
    xiangqing: []
  },
  mutations: {
    add(state, action) {
      console.log(action.goods_id, '22222');
      const index = state.ShopList.findIndex(item => (item.goods_id == action.goods_id));
      // console.log(flag);
      if (index === -1) {
        action.count = 1;
        action.flag = true;
        state.ShopList.push(action);
      } else {
        state.ShopList[index].count++;
      }
      console.log(state.ShopList);
    },
    addCount(state, action) {
      action.count++;
    },
    delCount(state, action) {
      action.count--;
    },
    del(state, action) {
      state.ShopList.splice(action, 1);
    },
    changeFlag(state, action) {
      console.log(action, 2233666)
      state.ShopList.forEach(item => {
        item.flag = action;
      });
    },
    toggleAlls(state, action) {
      state.ShopList.forEach(item => {
        item.flag = !item.flag;
      });
    },
    some(state, action) {
     const checked = state.ShopList.every(item => {
         item.flag = true;
      });
      console.log(checked);
    },
    addItem(state, action) {
       state.xiang.unshift(action);
       state.xiangqing = state.xiang.splice(0, 1);
    }
  },
  actions: {
  },
  modules: {
    patientStore: patientStore,
    doctorStore: doctorStore
  },
  getters: {
    allCount(state) {
      return state.ShopList.reduce((prev, next) => {
        if (next.flag) {
          return prev + next.count
        }
         return prev;
      }, 0);
    },
    allMoney(state) {
      return state.ShopList.reduce((prev, next) => {
        if (next.flag) {
          return prev + next.count * next.goods_price
        }
        return prev;
      }, 0);
    }
  }
});
